<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Get\Send;
use App\Models\CollArea;
use App\Models\CompanyUser;
use App\Models\Product;
use App\Models\ProductOrder;
use App\Models\Resume;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use App\Admin\Actions\Post\Restore;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Request;


class ProductOrderController extends AdminController
{
    protected $title = '订单列表';

    private static $statusArr = [
        '0' => '待支付',
        '1' => '已支付',
        '10' => '已发货',
        '20' => '已签收'
    ];

    protected function grid()
    {
        $status = Request::get('status', 0);
        $grid = new Grid(new ProductOrder());
        $grid->model()->where('status', $status);

        $grid->column('id', __('Id号'));
        $grid->column('order_number', __('订单编号'))->expand(function ($model) {

            $lists = $model->product_list()->get()->map(function ($list) {
                $list->product_id = Product::where('id', $list->product_id)->value('product_name');
                $list->create_time = date('Y-m-d H:i:s', $list->create_time);
                $list->pay_time = $list->pay_time > 0 ? date('Y-m-d H:i:s', $list->pay_time) : '尚未支付';
                $list->send_time = $list->send_time > 0 ? date('Y-m-d H:i:s', $list->send_time) : '尚未发货';
                $list->finish_time = $list->finish_time > 0 ? date('Y-m-d H:i:s', $list->finish_time) : '尚未签单';
                return $list->only(['id', 'order_number', 'product_id', 'price', 'num', 'total', 'create_time', 'pay_time', 'send_time', 'finish_time']);
            });

            return new Table(['ID', '订单号', '商品名称', '价格', '数量', '总额', '创建时间', '支付时间', '发货时间', '完成时间'], $lists->toArray());
        });
        $grid->column('total', __('商品总额'));
        $grid->column('transfee', '商品运费');
        $grid->column('all', __('订单总额'));
        $grid->column('status', __('订单状态'))->using(self::$statusArr);
        $grid->column('uid', __('用户昵称'))->display(function ($uid) {
            return User::where('uid', $uid)->value('nickname');
        });
        $grid->column('create_time', __('创建时间'))->display(function ($create_time) {
            return date('Y-m-d H:i:s', $create_time);
        });
        $grid->column('pay_time', __('支付时间'))->display(function ($pay_time) {
            if ($pay_time > 0) {
                return date('Y-m-d H:i:s', $pay_time);
            } else {
                return '尚未支付';
            }
        });

        $grid->column('send_time', __('发货时间'))->display(function ($send_time) {
            if ($send_time > 0) {
                return date('Y-m-d H:i:s', $send_time);
            } else {
                return '尚未发货';
            }
        });

        $grid->column('finish_time', __('签单时间'))->display(function ($finish_time) {
            if ($finish_time > 0) {
                return date('Y-m-d H:i:s', $finish_time);
            } else {
                return '尚未签单';
            }
        });

        $grid->disableCreateButton();
        $grid->disableColumnSelector();

        $grid->filter(function ($filter) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->where(function ($query) {

                $uidsObj = User::where('nickname', 'like', "%{$this->input}%")->pluck('uid');
                $uidsArr = [];
                if ($uidsObj) {
                    foreach ($uidsObj as $v) {
                        array_unshift($uidsArr, $v);
                    }
                }

                $uids = array_unique($uidsArr);

                $query->whereIn('uid', $uids);

            }, '用户昵称');

        });

        $grid->actions(function ($actions) {
            // 去掉删除
            $actions->disableDelete();
            // 去掉编辑
            $actions->disableEdit();
        });

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(ProductOrder::findOrFail($id));

        $show->field('order_number', __('订单编号'));
        $show->field('total', __('商品总额'));
        $show->field('transfee', __('商品运费'));
        $show->field('all', __('订单总额'));
        $show->field('uid', __('用户昵称'))->as(function ($uid) {
            return User::where('uid', $uid)->value('nickname');
        });
        $show->field('create_time', __('创建时间'))->as(function ($create_time) {
            return date('Y-m-d H:i:s', $create_time);
        });
        $show->field('pay_time', __('支付时间'))->as(function ($pay_time) {
            if ($pay_time > 0) {
                return date('Y-m-d H:i:s', $pay_time);
            } else {
                return '尚未支付';
            }
        });
        $show->field('send_time', __('发货时间'))->as(function ($send_time) {
            if ($send_time > 0) {
                return date('Y-m-d H:i:s', $send_time);
            } else {
                return '尚未发货';
            }
        });
        $show->field('finish_time', __('签单时间'))->as(function ($finish_time) {
            if ($finish_time > 0) {
                return date('Y-m-d H:i:s', $finish_time);
            } else {
                return '尚未签单';
            }
        });

        $show->shipping_address('地址信息', function ($address) {

            $address->realname('联系人');
            $address->mobile('联系电话');

            $address->area('省市区')->as(function ($area) {
                return CollArea::getAreaAddress($area);
            });
            $address->address('具体地址');

            $address->panel()
                ->tools(function ($tools) {
                    $tools->disableEdit();
                    $tools->disableList();
                    $tools->disableDelete();
                });;

        });

        $show->product_list('商品清单', function ($vals) {
            $vals->id('ID号');
            $vals->product_id('商品名称')->display(function ($product_id) {
                return Product::where('id', $product_id)->value('product_name');
            });
            $vals->order_number('订单号');
            $vals->price('价格');
            $vals->num('数量');
            $vals->total('总额');

            $vals->create_time('创建时间')->display(function ($create_time) {
                return date('Y-m-d H:i:s', $create_time);
            });
            $vals->pay_time('支付时间')->display(function ($pay_time) {
                if ($pay_time > 0) {
                    return date('Y-m-d H:i:s', $pay_time);
                } else {
                    return '尚未支付';
                }
            });
            $vals->send_time('发货时间')->display(function ($send_time) {
                if ($send_time > 0) {
                    return date('Y-m-d H:i:s', $send_time);
                } else {
                    return '尚未发货';
                }
            });
            $vals->finish_time('签单时间')->display(function ($finish_time) {
                if ($finish_time > 0) {
                    return date('Y-m-d H:i:s', $finish_time);
                } else {
                    return '尚未签单';
                }
            });

            $vals->disableCreateButton();
            $vals->disableFilter();
            $vals->disableRowSelector();
            $vals->disableColumnSelector();
            $vals->disableExport();

            $vals->actions(function ($actions) {
                // 去掉删除
                $actions->disableDelete();
                // 去掉编辑
                $actions->disableEdit();
                // 去掉查看
                $actions->disableView();

                $info = $actions->row;
                if ($info->status == 1) {
                    $actions->add(new Send());
                }
            });
        });

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
                $tools->disableDelete();
            });

        return $show;
    }

}
